home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2008 February / PC_Format_022008.iso / Internet / Mozilla Thunderbird wtyczki / lightning-0.7-tb-win.xpi / components / lightningTextCalendarConverter.js < prev    next >
Encoding:
JavaScript  |  2007-09-22  |  9.2 KB  |  240 lines

  1. /* -*- Mode: javascript; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Lightning code.
  16.  *
  17.  * The Initial Developer of the Original Code is Oracle Corporation
  18.  * Portions created by the Initial Developer are Copyright (C) 2005
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Mike Shaver <shaver@mozilla.org>
  23.  *   Clint Talbert <ctalbert.moz@gmail.com>
  24.  *   Matthew Willis <lilmatt@mozilla.com>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. function makeTableRow(val) {
  41.     return "<tr><td>" + val[0] + "</td><td>" + val[1] + "</td></tr>\n";
  42. }
  43.  
  44. function getLightningStringBundle()
  45. {
  46.     var svc = Components.classes["@mozilla.org/intl/stringbundle;1"].
  47.               getService(Components.interfaces.nsIStringBundleService);
  48.     return svc.createBundle("chrome://lightning/locale/lightning.properties");
  49. }
  50.  
  51. function createHtmlTableSection(label, text)
  52. {
  53.     var tblRow = <tr>
  54.                     <td class="description">
  55.                         <p>{label}</p>
  56.                     </td>
  57.                     <td class="content">
  58.                         <p>{text}</p>
  59.                     </td>
  60.                  </tr>;
  61.     return tblRow;
  62. }
  63.  
  64. function createHtml(event)
  65. {
  66.     // Creates HTML using the Node strings in the properties file
  67.     var stringBundle = getLightningStringBundle();
  68.     var html;
  69.     if (stringBundle) {
  70.         // Using e4x javascript support here
  71.         html =
  72.                <html>
  73.                <head>
  74.                     <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
  75.                     <link rel='stylesheet' type='text/css' href='chrome://lightning/skin/imip.css'/>
  76.                </head>
  77.                <body>
  78.                     <table>
  79.                     </table>
  80.                </body>
  81.                </html>;
  82.         // Create header row
  83.         var labelText = stringBundle.GetStringFromName("imipHtml.header");
  84.         html.body.table.appendChild(
  85.             <tr>
  86.                 <td colspan="3" class="header">
  87.                     <p class="header">{labelText}</p>
  88.                 </td>
  89.             </tr>
  90.         );
  91.         if (event.title) {
  92.             labelText = stringBundle.GetStringFromName("imipHtml.summary");
  93.             html.body.table.appendChild(createHtmlTableSection(labelText,
  94.                                                                event.title));
  95.         }
  96.  
  97.         var eventLocation = event.getProperty("LOCATION");
  98.         if (eventLocation) {
  99.             labelText = stringBundle.GetStringFromName("imipHtml.location");
  100.             html.body.table.appendChild(createHtmlTableSection(labelText,
  101.                                                                eventLocation));
  102.         }
  103.  
  104.         var labelText = stringBundle.GetStringFromName("imipHtml.when");
  105.         html.body.table.appendChild(createHtmlTableSection(labelText,
  106.                                                            event.startDate.jsDate.toLocaleString()));
  107.  
  108.         if (event.organizer &&
  109.             (event.organizer.commonName || event.organizer.id))
  110.         {
  111.             labelText = stringBundle.GetStringFromName("imipHtml.organizer");
  112.             // Trim any instances of "mailto:" for better readibility.
  113.             var orgname = event.organizer.commonName ||
  114.                           event.organizer.id.replace(/mailto:/ig, "");
  115.             html.body.table.appendChild(createHtmlTableSection(labelText, orgname));
  116.         }
  117.  
  118.         var eventDescription = event.getProperty("DESCRIPTION");
  119.         if (eventDescription) {
  120.             // Remove the useless "Outlookism" squiggle.
  121.             var desc = eventDescription.replace("*~*~*~*~*~*~*~*~*~*", "");
  122.  
  123.             labelText = stringBundle.GetStringFromName("imipHtml.description");
  124.             html.body.table.appendChild(createHtmlTableSection(labelText,desc));
  125.         }
  126.     }
  127.  
  128.     return html;
  129. }
  130.  
  131. function ltnMimeConverter() { }
  132.  
  133. ltnMimeConverter.prototype = {
  134.     QueryInterface: function QI(aIID) {
  135.         if (!aIID.equals(Components.interfaces.nsISupports) &&
  136.             !aIID.equals(Components.interfaces.nsISimpleMimeConverter))
  137.         {
  138.             throw Components.results.NS_ERROR_NO_INTERFACE;
  139.         }
  140.  
  141.         return this;
  142.     },
  143.  
  144.     mUri: null,
  145.     get uri() {
  146.         return this.mUri;
  147.     },
  148.     set uri(aUri) {
  149.         return (this.mUri = aUri);
  150.     },
  151.  
  152.     convertToHTML: function lmcCTH(contentType, data) {
  153.         var event = Components.classes["@mozilla.org/calendar/event;1"].
  154.                     createInstance(Components.interfaces.calIEvent);
  155.         event.icalString = data;
  156.         var html = createHtml(event);
  157.  
  158.         try {
  159.             var itipItem = Components.classes["@mozilla.org/calendar/itip-item;1"].
  160.                            createInstance(Components.interfaces.calIItipItem);
  161.             itipItem.init(data);
  162.  
  163.             // this.mUri is the message URL that we are processing.
  164.             // We use it to get the nsMsgHeaderSink to store the calItipItem.
  165.             if (this.mUri) {
  166.                 var msgUrl = this.mUri.QueryInterface(Components.interfaces.nsIMsgMailNewsUrl);
  167.                 var sinkProps = msgUrl.msgWindow.msgHeaderSink.properties;
  168.                 sinkProps.setPropertyAsInterface("itipItem", itipItem);
  169.             
  170.                 // Notify the observer that the itipItem is available
  171.                 var observer = Components.classes["@mozilla.org/observer-service;1"].
  172.                                getService(Components.interfaces.nsIObserverService);
  173.                 observer.notifyObservers(null, "onItipItemCreation", 0);
  174.             } else {
  175.                 // Thunderbird 1.5.x case: We have no choice but to try
  176.                 // sending the iTIP item directly with the notification
  177.                 var observer = Components.classes["@mozilla.org/observer-service;1"].
  178.                                getService(Components.interfaces.nsIObserverService);
  179.                 observer.notifyObservers(itipItem, "onItipItemCreation", 0);
  180.             }
  181.  
  182.         } catch (e) {
  183.             Components.utils.reportError("convertToHTML: " +
  184.                                          "Cannot create itipItem: " + e);
  185.         }
  186.  
  187.         return html;
  188.     }
  189. };
  190.  
  191. var myModule = {
  192.     registerSelf: function RS(aCompMgr, aFileSpec, aLocation, aType) {
  193.         debug("*** Registering Lightning text/calendar handler\n");
  194.         var compMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  195.         compMgr.registerFactoryLocation(this.myCID,
  196.                                         "Lightning text/calendar handler",
  197.                                         this.myContractID,
  198.                                         aFileSpec,
  199.                                         aLocation,
  200.                                         aType);
  201.  
  202.         var catman = Components.classes["@mozilla.org/categorymanager;1"]
  203.                                .getService(Components.interfaces.nsICategoryManager);
  204.  
  205.         catman.addCategoryEntry("simple-mime-converters", "text/calendar",
  206.                                 this.myContractID, true, true);
  207.     },
  208.  
  209.     getClassObject: function GCO(aCompMgr, aCid, aIid) {
  210.         if (!aCid.equals(this.myCID)) {
  211.             throw Components.results.NS_ERROR_NO_INTERFACE;
  212.         }
  213.         if (!aIid.equals(Components.interfaces.nsIFactory)) {
  214.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  215.         }
  216.         return this.myFactory;
  217.     },
  218.  
  219.     myCID: Components.ID("{c70acb08-464e-4e55-899d-b2c84c5409fa}"),
  220.  
  221.     myContractID: "@mozilla.org/lightning/mime-converter;1",
  222.  
  223.     myFactory: {
  224.         createInstance: function mfCI(aOuter, aIid) {
  225.             if (aOuter != null) {
  226.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  227.             }
  228.             return (new ltnMimeConverter()).QueryInterface(aIid);
  229.         }
  230.     },
  231.  
  232.     canUnload: function CU(aCompMgr) {
  233.         return true;
  234.     }
  235. };
  236.  
  237. function NSGetModule(compMgr, fileSpec) {
  238.     return myModule;
  239. }
  240.